// Copyright 2017 The Closure Rules Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package io.bazel.rules.closure.worker.testing;

import io.bazel.rules.closure.worker.Annotations.Action;
import io.bazel.rules.closure.worker.ErrorReporter;
import io.bazel.rules.closure.worker.Program;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.inject.Inject;

/** Wrapper around a program that returns {@link ProgramResult} afterwards. */
public final class ProgramRunner<T extends Program> {

  private final Program delegate;
  private final AtomicBoolean failed;
  private final ErrorReporter reporter;

  @Inject
  ProgramRunner(T delegate, ErrorReporter reporter, @Action AtomicBoolean failed) {
    this.delegate = delegate;
    this.reporter = reporter;
    this.failed = failed;
  }

  /** Runs program and returns result of invocation. */
  public ProgramResult run() throws Exception {
    delegate.run();
    return new AutoValue_ProgramResult(reporter.getErrors(), reporter.getWarnings(), failed.get());
  }
}
